import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common";
import { AuthModule } from "./modules/auth/auth.module";
import { UserModule } from "./modules/user/user.module";
import { TypeOrmModule } from "@nestjs/typeorm";
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from "@nestjs/core";
import { JwtAuthGuard } from "src/utils/guards/jwt-auth.guard";
import { RolesGuard } from "./utils/guards/roles.guard";
import { getConnectionOptions } from "typeorm";
import { CompanyModule } from "./modules/company/company.module";
import { CompanyGuard } from "./utils/guards/company.guard";
import { ConfigModule } from "nestjs-config";
import * as path from "path";
import { NotFoundFilter } from "./utils/filter/not-found.filter";
import { DuplicateFilter } from "./utils/filter/duplicate.filter";
import { ClientModule } from "./modules/client/client.module";
import { OrderModule } from "./modules/order/order.module";
import { TypeModule } from "./modules/type/type.module";
import { QualityModule } from "./modules/quality/quality.module";
import { DeliveryModule } from "./modules/delivery/delivery.module";
import { LogModule } from "./modules/log/log.module";
import { FeedbackModule } from "./modules/feedback/feedback.module";
import { BankModule } from "./modules/bank/bank.module";
import { StatisticModule } from "./modules/statistics/statistic.module";
import { DeliveryItemModule } from "./modules/delivery-item/delivery-item.module";
import { DeliverOrderModule } from "./modules/deliver-order/deliver-order.module";
import { LogInterceptor } from "./modules/log/log.interceptor";
import { ScheduleModule } from "@nestjs/schedule";
import { LoggerMiddleware } from "./utils/middleware/logger.middleware";

@Module({
  // 注册Auth和Users模块，现在AppModule中的Controller和Service都可以访问这两个模块export的方法了
  imports: [
    AuthModule,
    UserModule,
    CompanyModule,
    TypeOrmModule.forRootAsync({
      useFactory: async () =>
        Object.assign(await getConnectionOptions(), {
          autoLoadEntities: true,
          synchronize: true,
        }),
    }),
    ConfigModule.load(path.resolve(__dirname, "config", "**/!(*.d).{ts,js}")),
    ClientModule,
    OrderModule,
    TypeModule,
    QualityModule,
    DeliveryModule,
    LogModule,
    FeedbackModule,
    BankModule,
    ScheduleModule.forRoot(),
    StatisticModule,
    DeliveryItemModule,
    DeliverOrderModule,
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: LogInterceptor,
    },
    {
      provide: APP_GUARD,
      useClass: RolesGuard,
    },
    {
      provide: APP_GUARD,
      useClass: CompanyGuard,
    },
    {
      provide: APP_FILTER,
      useClass: NotFoundFilter,
    },
    {
      provide: APP_FILTER,
      useClass: DuplicateFilter,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes("*");
  }
}
